package com.example.travel.common;


/**
 * 操作消息提醒
 */
public class AjaxResult<T>
{
    private Integer code;

    private String msg;

    private T data;

    /**
     * 构造方法私有化，不允许外部new
     * @param data
     */
    private AjaxResult(T data){
        this.code = 200;
        this.msg = "操作成功";
        this.data = data;
    }

    /**
     * 构造方法私有化，不允许外部new
     */
    private AjaxResult(String msg, Integer code){
        this.code = code;
        this.msg = msg;
    }

    /**
     * 成功时调用
     * @param
     * @param <T>
     * @return
     */
    public static <T> AjaxResult<T> success(){
        return success(null);
    }
    /**
     * 成功时调用
     * @param data
     * @param <T>
     * @return
     */
    public static <T> AjaxResult<T> success(T data){
        return new AjaxResult<T>(data);
    }
    /**
     * 失败时调用
     * @param
     * @param <T>
     * @return
     */
    public static <T> AjaxResult error(){
        return new AjaxResult<T>("操作失败", 500);
    }
    /**
     * 失败时调用
     * @param <T>
     * @return
     */
    public static <T> AjaxResult error(String msg){
        return new AjaxResult<T>(msg, 500);
    }

    public static <T> AjaxResult error(Integer code, String msg){
        return new AjaxResult<T>(msg, code);
    }

    public Integer getCode() {
        return code;
    }

    public AjaxResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public AjaxResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public AjaxResult<T> setData(T data) {
        this.data = data;
        return this;
    }
}
